package com.abso.mp3tunes.locker.ui.actions;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.abso.mp3tunes.locker.core.data.Track;
import com.abso.mp3tunes.locker.ui.LockerImages;
import com.abso.mp3tunes.locker.ui.viewers.LockerViewer;

/**
 * Action able to open the Last.fm page associated with a specific track.
 */
public class OpenLastFMTrackPageAction extends AbstractOpenBrowserAction {

	/** The selected track. */
	private Track track;

	/**
	 * Constructs a new action.
	 * 
	 * @param lockerViewer
	 *            the Locker tree viewer.
	 */
	public OpenLastFMTrackPageAction(LockerViewer lockerViewer) {
		super("Open Last.fm Page", LockerImages.getDescriptor(LockerImages.LAST_FM),
				lockerViewer);
	}

	/**
	 * Sets the current track.
	 * 
	 * @param track
	 *            the selected track.
	 */
	public void setSelectedTrack(Track track) {
		this.track = track;
	}

	/* (non-Javadoc)
	 * @see com.abso.mp3tunes.locker.ui.actions.AbstractOpenBrowserAction#getURL()
	 */
	protected String getURL() {
		try {
			return "http://www.last.fm/music/" + URLEncoder.encode(track.getArtistName(), "UTF-8") + "/_/" + URLEncoder.encode(track.getTitle(), "UTF-8") ;
		} catch (UnsupportedEncodingException e) {
			return "http://www.last.fm/music/" + track.getArtistName() + '/' + track.getTitle();
		}
	}

}
